<?php
class Admin_Model_Faq extends Zend_Db_Table{
	protected $_name = 'faq';
	protected $_primary = 'faqid';
	
	public function itemInSelectbox($arrParam = null, $option = null){
		$db = Zend_Registry::get('connectDb');
		if($option == null){
			$select = $db->select()
						 ->from('user_group', array('id', 'group_name'));
			$result = $db->fetchPairs($select);
			$result[0] = ' -- Select an Item -- ';
			ksort($result);
		}
		
		return $result;
	}
	
	public function countItem($arrParam = null, $option = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter = $arrParam['ssFilter'];
		
		$select = $db->select()
					 ->from('faq AS f', array('COUNT(f.faqid) AS totalItem'));
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('f.title_vi LIKE ? OR f.title LIKE ? ', $keywords, $keywords); // doi so thu 3 ep kieu STRING
		}
		
		$result = $db->fetchOne($select);
		return $result;
	}
	
	public function listItem($arrParam = null, $option = null){
//            echo "<pre>"; print_r($arrParam); die();
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		
		if($option['task'] == 'admin-list'){
			$select = $db->select()
                                     ->from('faq AS f');
			
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
                            
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
			
			
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('title LIKE ? OR title_vi LIKE ?', $keywords, $keywords); // doi so thu 3 ep kieu STRING
			}
			
			$result = $db->fetchAll($select);
		}
		
		return $result;
                
	}
	
	public function saveItem($arrParam = null, $option = null){
		
		if($option['task'] == 'admin-add'){
			$row                    = $this->fetchNew();
			$row->title             = $arrParam['title'];
                        $row->title_vi          = $arrParam['title_vi'];
                        $row->content           = $arrParam['content'];
                        $row->content_vi        = $arrParam['content_vi'];
                        $row->status            = $arrParam['status'];
                        $row->order             = $arrParam['order'];
			$row->datecreated	= date("Y-m-d H:i:s");
			$row->uidcreated 	= $arrParam['userinfor']['username'];
			
			$row->save();
		}
		if($option['task'] == 'admin-edit'){
			$where                  = 'faqid = ' . $arrParam['id'];
			$row                    = $this->fetchRow($where);
                        $row->title             = $arrParam['title'];
                        $row->title_vi          = $arrParam['title_vi'];
                        $row->content           = $arrParam['content'];
                        $row->content_vi        = $arrParam['content_vi'];
                        $row->status            = $arrParam['status'];
                        $row->order             = $arrParam['order'];
			$row->dateupdated	= date("Y-m-d H:i:s");
			$row->uidupdated 	= $arrParam['userinfor']['username'];
			$row->save();
		}
	}
	
	public function getItem($arrParam = null, $option = null){
		
		if($option['task'] == 'admin-info' || $option['task'] == 'admin-edit'){
			$where = 'faqid = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		
		return $result;
	}
	
	public function deleteItem($arrParam = null, $option = null){
		
		if($option['task'] == 'admin-delete'){
			$where = 'faqid = ' .$arrParam['id'];
			$this->delete($where);
		}
		
		if($option['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){
				if($arrParam['type'] == 1){
					$status = 1;
				}else{
					$status = 0;
				}
				$ids = implode(',', $cid);
				$where = 'faqid IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	
	public function changeStatua($arrParam = null, $option = null){
                
		$cid = $arrParam['cid'];
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$ids = implode(',', $cid);
			$data = array('status'=>$status);
			$where = 'faqid IN (' . $ids . ')';
			$this->update($data, $where);
			// UPDATE FROM user_group SET status = 1 WHERE id IN (1,2,3)
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$data = array('status'=>$status);
			$where = 'faqid = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	public function changeAcp($arrParam = null, $option = null){
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$statusAcp = 1;
			}else{
				$statusAcp = 0;
			}
				
			$data = array('group_acp'=>$statusAcp);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	public function sortItem($arrParam = null, $option = null){
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if(count($cid)>0){
			foreach($cid as $key=>$val){
				$data = array('order'=>$order[$val]);
				$where = 'faqid = ' . $val['cid'];
				$this->update($data, $where);
			}
		}
	}
}